package com.example.dangdangbackend.service.impl;

import com.example.dangdangbackend.DTO.LoginDTO;
import com.example.dangdangbackend.DTO.RegisterDTO;
import com.example.dangdangbackend.common.CustomException;
import com.example.dangdangbackend.entity.Result;
import com.example.dangdangbackend.entity.User;
import com.example.dangdangbackend.mapper.UserMapper;
import com.example.dangdangbackend.service.UserService;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.Random;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JavaMailSenderImpl mailSender;

    /**
     * 获取所有的用户信息
     * @return
     */
    @Override
    public List<User> getUsers() {
        return userMapper.getUsers();
    }

    @Override
    public User UserLogin(LoginDTO loginDTO) {
        String email = loginDTO.getEmail();
        String password = loginDTO.getPassword();

        //根据email查询数据库中的数据
        User user = userMapper.selectByEmail(email,password);

        if(user==null){
            //处理异常操作
            throw new CustomException("用户名或密码错误");
        }

        //密码对比 对前端的密码进行md5加密 后端的密码也进行md5加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if(!password.equals(user.getPassword())){
            throw new CustomException("密码错误");
        }

        return user;
    }

    @Override
    public boolean Register(RegisterDTO registerDTO,String Code) {

        //非空判断
        if(registerDTO==null){
            throw new CustomException("当前注册人员为空");
        }

        //验证码校验
        if(!registerDTO.getCode().equals(Code)){
            throw new CustomException("验证码错误");
        }

        User u = new User();
        u.setEmail(registerDTO.getEmail());
        u.setNickName(registerDTO.getNickName());

        String password = DigestUtils.md5DigestAsHex(registerDTO.getPassword().getBytes());
        u.setPassword(password);

        userMapper.register(u);
        return true;
    }

    /**
     * 向用户邮箱发送验证码
     * @param email
     * @return
     */
    @Override
    public String sendEmail(String email) throws MessagingException {
        int count=1;//默认发送一次
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
        StringBuilder codeNum = new StringBuilder();
        while(count--!=0) {
            int[] code = new int[3];
            Random random = new Random();
            //自动生成验证码
            for (int i = 0; i < 6; i++) {
                int num = random.nextInt(10) + 48;
                int uppercase = random.nextInt(26) + 65;
                int lowercase = random.nextInt(26) + 97;
                code[0] = num;
                code[1] = uppercase;
                code[2] = lowercase;
                codeNum.append((char) code[random.nextInt(3)]);
            }

            //标题
            helper.setSubject("您的验证码为：" + codeNum);
            //内容
            helper.setText("您好！，感谢您使用当当网。您的验证码为：" + "<span style='background-color: lightgrey;'>" + codeNum + "</span>" + "千万不能告诉别人哦！", true);            //邮件接收者
            helper.setTo(email);
            //邮件发送者，必须和配置文件里的一样，不然授权码匹配不上
            helper.setFrom("329677803@qq.com");
            mailSender.send(mimeMessage);
        }
        return codeNum.toString();
    }

    /**
     * 判断当前的用户的email是否存在
     * @param email
     * @return
     */
    @Override
    public boolean ifExist(String email) {
        if(email==null){
            throw new CustomException("当前的邮箱为空");
        }

        int count = userMapper.ifExist(email);
        return count > 0;
    }
}
